import 'package:deepseek_desktop/entity/ChatMessage.dart';
import 'package:file_picker/file_picker.dart';
import 'dart:typed_data';

class ChatItem {
  String id;
  String name;
  String avatar;
  String subtitle;
  String system;
  List<String> plugins;
  double temperature;
  int dialogCount;
  List<ChatMessage> history;
  List<PlatformFile> selectedAttachments;
  String lastMessage;
  String lastMessageTime;

  ChatItem(
      {required this.id,
      required this.name,
      required this.avatar,
      this.subtitle = "",
      this.system = "",
      this.plugins = const [],
      this.dialogCount = 5,
      this.temperature = 0.8,
      this.history = const [],
      this.selectedAttachments = const [],
      required this.lastMessage,
      required this.lastMessageTime});

  // toJson
  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'avatar': avatar,
        'subtitle': subtitle,
        'system': system,
        'plugins': plugins,
        'temperature': temperature,
        'dialogCount': dialogCount,
        'history': history,
        'selectedAttachments': selectedAttachments
            .map((file) => {
                  'name': file.name,
                  'size': file.size,
                  // 'bytes': file.bytes?.toList(), // 转换字节数据
                  'path': file.path,
                })
            .toList(),
        'lastMessage': lastMessage,
        'lastMessageTime': lastMessageTime,
      };

  // fromJson
  static ChatItem fromJson(Map<String, dynamic> json) {
    return ChatItem(
      id: json['id'],
      name: json['name'],
      avatar: json['avatar'],
      subtitle: json['subtitle'],
      system: json['system'],
      plugins: List<String>.from(json['plugins']),
      temperature: json['temperature'],
      dialogCount: json['dialogCount'],
      history: List<ChatMessage>.from(
          json['history'].map((item) => ChatMessage.fromJson(item))),
      selectedAttachments: List<PlatformFile>.from(
          (json['selectedAttachments'] ?? []).map((f) => PlatformFile(
                name: f['name'] ?? '',
                size: f['size'] ?? 0,
                // bytes: f['bytes'] != null
                //     ? Uint8List.fromList(
                //         List<int>.from(f['bytes'] as List)) // 更安全的类型转换
                //     : null,
                path: f['path'] ?? '',
              ))),
      lastMessage: json['lastMessage'],
      lastMessageTime: json['lastMessageTime'],
    );
  }
}
